#include "stdafx.h"
#include "Util.h"

using namespace xBase;

std::string xBase::WideCharToMultiByte(const WCHAR* szSrc, UINT nCodePage)
{
    if (NULL == szSrc)
    {
        return NULL;
    }

    INT nLen = ::WideCharToMultiByte(nCodePage,
        0,
        szSrc,
        -1,
        NULL,
        0,
        NULL,
        NULL
        );

    if (0 == nLen)
    {
        return NULL;
    }

    CHAR* pNewBuf = new (std::nothrow) CHAR[static_cast<unsigned int>(nLen + 1)];

    if (!pNewBuf)
    {
        assert(!L"alloc memory failure!!!");
        return NULL;
    }

    nLen = ::WideCharToMultiByte(nCodePage,
        0,
        szSrc,
        -1,
        pNewBuf,
        nLen,
        NULL,
        NULL
        );

    std::string strRet(pNewBuf);
    delete []pNewBuf;

    return strRet;
}

std::wstring xBase::MultiByteToWideChar(const CHAR* szSrc, UINT nCodePage)
{
    if (NULL == szSrc)
    {
        return NULL;
    }

    INT nLen = ::MultiByteToWideChar(nCodePage,
        0,
        szSrc,
        -1,
        NULL,
        0
        );

    if (0 == nLen)
    {
        return NULL;
    }

    WCHAR* pNewBuf = new (std::nothrow) WCHAR[static_cast<unsigned int>(nLen + 1)];

    if (!pNewBuf)
    {
        assert(!L"alloc memory failure!!!");
        return NULL;
    }

    nLen = ::MultiByteToWideChar(nCodePage,
        0,
        szSrc,
        -1,
        pNewBuf,
        nLen
        );

    std::wstring wstrRet(pNewBuf);
    delete []pNewBuf;

    return wstrRet;
}

UINT xBase::Time33HashMethod(const TCHAR* const pText) 
{
    if (!pText)
    {
        return -1;
    }

    const TCHAR* p = pText;

    UINT hash_key = 0;

    while (p && *p)
    {
        hash_key = hash_key * 33 + (UINT) (*p); 
        ++p;
    }

    return hash_key; 
}

INT xBase::WCHAR2INT(const wchar_t* const pchSrc)
{
    return _wtoi(pchSrc);
}

BOOL xBase::WCHAR2BOOL(const wchar_t* const pchSrc)
{
    CString strValue = pchSrc;
    return (0 == strValue.CompareNoCase(L"true"));
}

BYTE xBase::WCHAR2BYTE(const wchar_t* const pchSrc)
{
    return (BYTE)(_wtoi(pchSrc));
}

COLORREF xBase::WCHAR2COLORREF(const wchar_t* const pchSrc)
{
    if (!pchSrc)
    {
        ATLASSERT(FALSE);
        return CLR_INVALID;
    }
    else
    {
        int r, g, b, a = 255;
        swscanf_s(pchSrc, L"%d,%d,%d,%d", &r, &g, &b, &a);
        return RGB(r, g, b) + (a << 24);
    }
}

RECT xBase::WCHAR2RECT(const wchar_t* const pchSrc)
{
    RECT rc = {0};

    if (NULL == pchSrc)
    {
        ATLASSERT(FALSE);
    }
    else
    {
        swscanf_s(pchSrc, L"%d,%d,%d,%d", &(rc.left), &(rc.top), &(rc.right), &(rc.bottom));
    }

    return rc;
}

POINT xBase::WCHAR2POINT(const wchar_t* const pchSrc)
{
    POINT pt = {0};

    if (NULL == pchSrc)
    {
        ATLASSERT(FALSE);
    }
    else
    {
        swscanf_s(pchSrc, L"%d,%d", &(pt.x), &(pt.y));
    }

    return pt;
}

SIZE xBase::WCHAR2SIZE(const wchar_t* const pchSrc)
{
    SIZE sz = {0};

    if (NULL == pchSrc)
    {
        ATLASSERT(FALSE);
    }
    else
    {
        swscanf_s(pchSrc, L"%d,%d", &(sz.cx), &(sz.cy));
    }

    return sz;
}